package com.log.core;


import com.log.core.constant.LogMessageConstant;
import com.log.core.disruptor.LogMessageProducer;
import com.log.core.disruptor.LogRingBuffer;
import com.log.core.dto.BaseLogMessage;
import com.log.core.dto.RunLogMessage;
import com.log.core.util.GfJsonUtil;
import com.log.core.util.ThreadPoolUtil;

import java.util.concurrent.ThreadPoolExecutor;


public class MessageAppenderFactory {


    private static ThreadPoolExecutor threadPoolExecutor
            = ThreadPoolUtil.getPool(4, 8, 5000);


    public static void push(BaseLogMessage baseLogMessage) {
        LogMessageProducer producer = new LogMessageProducer(LogRingBuffer.ringBuffer);
        producer.send(baseLogMessage);
    }


    public static void push(BaseLogMessage baseLogMessage, AbstractClient client) {
        final String redisKey =
                baseLogMessage instanceof RunLogMessage
                        ? LogMessageConstant.LOG_KEY
                        : LogMessageConstant.LOG_KEY_TRACE;
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                client.pushMessage(redisKey, GfJsonUtil.toJSONString(baseLogMessage));
            }
        });
    }

}
